﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bonit_It.ViewModel.Cashier
{
    using System.Windows.Input;

    using Bon_It.Model.TransferObjects;

    public partial class CashierViewModel
    {

       
        #region Commands

        public ICommand CancelCommand { get; set; }

        /// <summary>
        /// Gets or sets the input command.
        /// </summary>
        /// <value>
        /// The input command.
        /// </value>
        public ICommand InputCommand { get; set; }

        /// <summary>
        /// Gets or sets the switch mode command.
        /// </summary>
        /// <value>
        /// The switch mode command.
        /// </value>
        public ICommand DebitCommand { get; set; }


        #endregion

        #region Command implementations

        #region DebitCommand

        /// <summary>
        /// Determines whether this instance [can debit command] the specified arg.
        /// </summary>
        /// <param name="arg">The arg.</param>
        /// <returns>
        ///   <c>true</c> if this instance [can debit command] the specified arg; otherwise, <c>false</c>.
        /// </returns>
        private bool CanDebitCommand(object arg)
        {
            // can only execute, when min one item exist.
            return Bookingengine.List.Count > 0;
        }


        public void ExecuteDebitCommand(object arg)
        {
            string command = arg.ToString();
            switch (CommandInputEngine.InputMode)
            {
                case CommandInputEngine.InputModes.ArticleSelection:
                    SetInputMode(CommandInputEngine.InputModes.Payment);
                    break;
                case CommandInputEngine.InputModes.Payment:
                    Bookingengine.Debit(command);
                    this.InputString = string.Empty;
                    SetInputMode(CommandInputEngine.InputModes.ArticleSelection);

                    break;

            }
        }

        #endregion


        #region Cancel implementation
        /// <summary>
        /// Determines whether this instance [can cancel command] the specified arg.
        /// </summary>
        /// <param name="arg">The arg.</param>
        /// <returns>
        ///   <c>true</c> if this instance [can cancel command] the specified arg; otherwise, <c>false</c>.
        /// </returns>
        private bool CanCancelCommand(object arg)
        {
            // can only perform if any item exist and the InputString is empty 
            return string.IsNullOrEmpty(InputString) && !Bookingengine.List.Count.Equals(0);
        }

        /// <summary>
        /// If the <see cref="CommandInputEngine.InputMode"/> is set to <see cref="CommandInputEngine.InputModes.Payment"/> it return to the <see cref="CommandInputEngine.InputModes.ArticleSelection"/>.
        /// If it is in the <see cref="CommandInputEngine.InputModes.ArticleSelection"/> it remoces the last article. 
        /// </summary>
        /// <param name="arg">The arg.</param>
        public void CancelCommandExecute(object arg)
        {
            switch (CommandInputEngine.InputMode)
            {
                case CommandInputEngine.InputModes.Payment:
                    SetInputMode(CommandInputEngine.InputModes.ArticleSelection);

                    
                    break;
                case CommandInputEngine.InputModes.ArticleSelection:

                    Bookingengine.RemoveLastArticle();
                    break;
            }
        }

        #endregion




        #region Input Implementation

        /// <summary>
        /// Determines whether this instance [can input command] the specified arg.
        /// </summary>
        /// <param name="arg">The arg.</param>
        /// <returns>
        ///   <c>true</c> if this instance [can input command] the specified arg; otherwise, <c>false</c>.
        /// </returns>
        private bool CanInputCommand(object arg)
        {
            return true;
        }

        /// <summary>
        /// Executes the input command.
        /// </summary>
        /// <param name="arg">The arg.</param>
        public void ExecuteInputCommand(object arg)
        {
            // also allow confirm the input with the enter key
            if (CommandInputEngine.InputMode == CommandInputEngine.InputModes.Payment)
            {
                this.ExecuteDebitCommand(DebitInformation.DebitTypes.Cash);
                return;
            }
            this.Bookingengine.Given = 0;
            this.Bookingengine.PayBack = 0;

            var item = this.InputEngine.GetBookingItem(InputString);
            if (item != null)
            {
                Bookingengine.AddArticle(item);
            }

            this.InputString = string.Empty;
        }

        #endregion


        #endregion
    }
}
